package com.kakavr.emp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.emp.service.IDeviceAdminService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.SessionUtils;
/**
 * 设备管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("device")
public class DeviceAdminController extends BaseController {
	@Resource
	private IDeviceAdminService deviceAdminService;
	
	/**
	 * 添加设备
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> map = this.getParam(request);
		deviceAdminService.addDevice(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 修改设备
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public Map<String, Object> update(HttpServletRequest request){
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "设备");
		}
		Map<String, Object> map = this.getParam(request);
		map.put("id", id);
		deviceAdminService.updateDevice(map);
		return MsgResult.okResult();
	}
	
	/**
	 * 获取前端参数
	 * @param request
	 * @return
	 */
	private Map<String,Object> getParam(HttpServletRequest request){
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "设备名称");
		}
		String macAddress = request.getParameter("macAddress");
		if (StringUtils.isBlank(macAddress)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "mac地址");
		}
		String remark = request.getParameter("remark");
		if (StringUtils.isBlank(remark)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "备注");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("name", name);
		map.put("macAddress", macAddress);
		map.put("remark", remark);
		map.put("storeId", SessionUtils.getShopId());
		return map;
	}
	
	/**
	 * 设备列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "select", method = RequestMethod.GET)
	public Map<String, Object> select(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("storeId", SessionUtils.getShopId());
		return MsgResult.okResultData(deviceAdminService.selectDeviceList(map));
	}
	
	/**
	 * 删除设备
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public Map<String, Object> delete(HttpServletRequest request){
		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "设备");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		map.put("storeId", SessionUtils.getShopId());
		deviceAdminService.deleteDevice(map);
		return MsgResult.okResult();
	}

}
